package com.zx.dsptuneup_TriMic.NodeTree;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.multilevel.treelist.Node;
import com.multilevel.treelist.TreeRecyclerAdapter;
import com.xujiaji.happybubble.BubbleDialog;
import com.xujiaji.happybubble.BubbleLayout;
import com.xujiaji.happybubble.Util;
import com.zx.dsptuneup_TriMic.Common.circleView.ArcControlView;
import com.zx.dsptuneup_TriMic.R;
import java.util.List;

public class MTreeRecycleAdapter extends TreeRecyclerAdapter {
    private  List<Node> mdatas;

    private MTreeRecycleAdapter.treeOperationInterface treeOperationClickListener;

    private OnItemClickListener mOnItemClickListener;
    // 是否显示后面的操作按钮,默认显示
    private boolean IsShowOperation = true;
    public void setShowOperation(boolean showOperation) {
        IsShowOperation = showOperation;
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onItemClicked(Node node, int position);
    }

    public void setOnItemClickListener(OnItemClickListener clickListener) {
        this.mOnItemClickListener = clickListener;
    }
    //  操作点击
    public interface treeOperationInterface {
         void treeOperationClick(View view,int position);
    }

    public MTreeRecycleAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel, int iconExpand, int iconNoExpand) {
        super(mTree, context, datas, defaultExpandLevel, iconExpand, iconNoExpand);
        mdatas = datas;
    }

    public MTreeRecycleAdapter(RecyclerView mTree, Context context, List<Node> datas, int defaultExpandLevel) {
        super(mTree, context, datas, defaultExpandLevel);
        mdatas = datas;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyHoder(View.inflate(mContext, R.layout.list_item,null));
    }

    @Override
    public void onBindViewHolder(final Node node, RecyclerView.ViewHolder holder, int position) {

        final MyHoder viewHolder = (MyHoder) holder;

        if (node.getIcon() == -1) {
            viewHolder.icon.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.icon.setVisibility(View.VISIBLE);
            viewHolder.icon.setImageResource(node.getIcon());
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 展开或者关闭，同时事件继续分发
                expandOrCollapse(position);
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClicked(mdatas.get(position),position);

                }
            }
        });

        viewHolder.label.setText(node.getName());
        if(IsShowOperation){
            viewHolder.operation.setVisibility(View.VISIBLE);
        }else {
            viewHolder.operation.setVisibility(View.GONE);
        }
        viewHolder.operation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                treeOperationClickListener.treeOperationClick(view,position);
            }
        });

    }

    public void setTreeOperationClickListener(treeOperationInterface treeOperationClickListener) {
        this.treeOperationClickListener = treeOperationClickListener;
    }

    public treeOperationInterface getTreeOperationClickListener() {
        return treeOperationClickListener;
    }

    class MyHoder extends RecyclerView.ViewHolder{

        public TextView label;
        public ImageView icon;
        public TextView operation;
        public MyHoder(View itemView) {
            super(itemView);
            label = (TextView) itemView
                    .findViewById(R.id.id_treenode_label);
            icon = (ImageView) itemView.findViewById(R.id.icon);
            operation = (TextView) itemView
                    .findViewById(R.id.operation);

        }

    }
}

